(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.glEntry.listController", listController);
	listController.$inject = [ "$scope", "gridService", "modalService", "layoutConfig", "validatorConfig", "authService",
			"glEntryService" ];
	function listController($scope, gridService, modalService, layoutConfig, validatorConfig, authService, glEntryService) {
		var ctrl = this;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;

		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = "vm.searchCriteria.list";
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			glEntryService.getSetlItemTypeOptions().then(function(response) {
				ctrl.itemTypeOptions = response;
			});
			// filter
			ctrl.glStatusOptions = [ {
				"value" : "C",
				"label" : "label.glentry.created"
			}, {
				"value" : "W",
				"label" : "label.glentry.waiting"
			}, {
				"value" : "S",
				"label" : "label.glentry.sent"
			}, {
				"value" : "R",
				"label" : "label.glentry.sentbutneedreverse"
			}, {
				"value" : "X",
				"label" : "label.glentry.reversed"
			} ];

			ctrl.glpostTypeOptions = [ {
				"value" : "N",
				"label" : "label.glentry.notsend"
			}, {
				"value" : "B",
				"label" : "label.glentry.batch"
			}, {
				"value" : "O",
				"label" : "label.glentry.online"
			} ];
			ctrl.search();
		};

		ctrl.view = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ "title.dialogs.info.norecsel" ]
				});
				return;
			}
			$scope.go("cls.glEntry.view", ctrl.selectedRows[0]);
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};
		
		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : "";
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : "";
			ctrl.search();
		}
	}
})(angular);